package cn.edu.cqvie.netty.grpc;

import cn.edu.cqvie.netty.proto.*;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.time.LocalDateTime;
import java.util.Iterator;

public class GrpcClient {

    public static void main(String[] args) throws InterruptedException {

        ManagedChannel channel = ManagedChannelBuilder
                .forAddress("127.0.0.1", 8899)
                .usePlaintext()
                .build();
        StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(channel);
        StudentServiceGrpc.StudentServiceStub stub = StudentServiceGrpc.newStub(channel);

        MyRespone respone = blockingStub.getRealNameByUsername(MyRequest.newBuilder().setUsername("张三").build());
        System.out.println(respone.getRealname());

        System.out.println("-----------------------");
        Iterator<StudentRespone> respone1 = blockingStub.getStudentsByAge(StudentRequest.newBuilder().setAge(20).build());
        while (respone1.hasNext()) {
            StudentRespone r = respone1.next();
            System.out.println("name=" + r.getName() + ";age="+ r.getAge() + ";city=" + r.getCity());
            System.out.println();
        }
        System.out.println("-----------------------");


        StreamObserver<StudentResponeList> studentResponeListStreamObserver = new StreamObserver<StudentResponeList>() {
            @Override
            public void onNext(StudentResponeList value) {
                value.getStudentResponeList().forEach( s ->  {
                    System.out.println(s.getName() + "," + s.getAge() + "," + s.getCity());
                });
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
            }

            @Override
            public void onCompleted() {
                System.out.println("completed!");
            }
        };

        StreamObserver<StudentRequest> studentRequestStreamObserver = stub.getStudentsWrapperByAge(studentResponeListStreamObserver);
        studentRequestStreamObserver.onNext(StudentRequest.newBuilder().setAge(20).build());
        studentRequestStreamObserver.onNext(StudentRequest.newBuilder().setAge(21).build());
        studentRequestStreamObserver.onNext(StudentRequest.newBuilder().setAge(22).build());
        studentRequestStreamObserver.onNext(StudentRequest.newBuilder().setAge(23).build());
        studentRequestStreamObserver.onCompleted();

        Thread.sleep(10000);
        System.out.println("-----------------------");
        StreamObserver<StreamRequest> studentRequestStreamObserver1 = stub.biTalk(new StreamObserver<StreamRespone>() {
            @Override
            public void onNext(StreamRespone value) {
                System.out.println(value.getResponeInfo());
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.getMessage());
            }

            @Override
            public void onCompleted() {
                System.out.println("complated!");
            }
        }) ;
        for (int i=0; i< 10; i++) {
            studentRequestStreamObserver1.onNext(StreamRequest.newBuilder().setRequestInfo(LocalDateTime.now().toString()).build());
            Thread.sleep(1000);
        }

        System.out.println("-----------------------");

//        MyRespone respone1 = blockingStub.getRealNameByUsername(MyRequest.newBuilder().setUsername("张三").build());
//        System.out.println(respone1.getRealname());
//
//        MyRespone respone2 = blockingStub.getRealNameByUsername(MyRequest.newBuilder().setUsername("张三").build());
//        System.out.println(respone2.getRealname());
//
//        MyRespone respone3 = blockingStub.getRealNameByUsername(MyRequest.newBuilder().setUsername("张三").build());
//        System.out.println(respone3.getRealname());
//
//        MyRespone respone4 = blockingStub.getRealNameByUsername(MyRequest.newBuilder().setUsername("张三").build());
//        System.out.println(respone4.getRealname());
//
//
//        MyRespone respone5 = blockingStub.getRealNameByUsername(MyRequest.newBuilder().setUsername("张三").build());
//        System.out.println(respone5.getRealname());
//
//        MyRespone respone6 = blockingStub.getRealNameByUsername(MyRequest.newBuilder().setUsername("张三").build());
//        System.out.println(respone6.getRealname());





    }
}
